package com.jiuyin.dianjing.ui.fragment.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.HomeBannerMadel;
import com.jiuyin.dianjing.ui.activity.gameInfo.GameInfoDetailActivity;
import com.jiuyin.dianjing.ui.activity.search.SearchGameInfoActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseTabFragment;
import com.jiuyin.dianjing.ui.fragment.first.FragmentFirstInnerFirst;
import com.jiuyin.dianjing.util.LogUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_HEAD_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INFORMATIONID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INFORMATION_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INFORMATION_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_SEARCH;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TITLE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_SEARCH_TYPE_INFO;
import static com.jiuyin.dianjing.api.enums.ApiEnum.APP_VIEW_PAGE_TOP_INFORMATION_LIST;
import static com.jiuyin.dianjing.constant.AppConstant.WEIBO_GONGYI;

/**
 * 主界面(资讯)
 */
public class FragmentFirst extends BaseTabFragment<Void> {
    @BindView(R.id.banner_guide_content)
    BGABanner mBgaBanner;

    @Override
    public void fetchData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        JSONObject jsonObject = new JSONObject(params);
        ServerApi.post(APP_VIEW_PAGE_TOP_INFORMATION_LIST.getUrl(), jsonObject, null, null, d -> addDisposable(d),
                new ApiResultCallback() {
                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        if (null != jsonObject && jsonObject.has(KEY_INFORMATION_LIST)) {
                            LogUtil.log("FragmentFirst appGetViewPage onSuccess = " + jsonObject.toString());
                            JsonArray jsonArray = jsonObject.getAsJsonArray(KEY_INFORMATION_LIST);
                            if (jsonArray != null) {
                                int size = jsonArray.size();
                                List<HomeBannerMadel> list = new ArrayList<>();
                                if (size > 0) {
                                    for (int i = 0; i < size; i++) {
                                        JsonObject object = jsonArray.get(i).getAsJsonObject();
                                        HomeBannerMadel bannerModel = new HomeBannerMadel();
                                        String informationId = object.get(KEY_INFORMATION_ID).getAsString();
                                        String title = object.get(KEY_TITLE).getAsString();
                                        String url = object.get(KEY_URL).getAsString();
                                        String headerUrl = object.get(KEY_HEAD_URL).getAsString();
                                        bannerModel.setInformation_id(informationId);
                                        bannerModel.setTitle(title);
                                        bannerModel.setUrl(url);
                                        bannerModel.setHeadurl(headerUrl);
                                        list.add(bannerModel);
                                    }
                                    mBgaBanner.setData(list, null);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        LogUtil.log("FragmentFirst appGetViewPage onFail = " + msg);
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.log("FragmentFirst appGetViewPage onError = " + msg);
                    }
                });
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_first;
    }

    @Override
    protected void initView() {
        mBgaBanner.setAdapter((BGABanner.Adapter<ImageView, HomeBannerMadel>) (banner, itemView, model, position) -> Glide.with(getContext())
                .load(model.getHeadurl())
                .placeholder(R.drawable.img_banner_placeholder)
                .into(itemView));
        mBgaBanner.setDelegate((BGABanner.Delegate<ImageView, HomeBannerMadel>) (banner, itemView, model, position) -> gotoBrowser(model));
        addData(null);
    }


    @OnClick({R.id.tv_search, R.id.fab})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_search:
                Bundle bundle = new Bundle();
                bundle.putString(KEY_SEARCH, getString(R.string.search_info_go));
                bundle.putString(KEY_TYPE, VALUE_SEARCH_TYPE_INFO);
                goTargetActivity(SearchGameInfoActivity.class, bundle);
                break;
            case R.id.fab:
                Uri uri = Uri.parse(WEIBO_GONGYI);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void addFragments(List<Void> list) {
        mFragments.add(Pair.create("", new FragmentFirstInnerFirst()));
//        mFragments.add(Pair.create(getString(R.string.first_text_nav_second), new FragmentFirstInnerSecond()));
    }

    /**
     * 跳转资讯详情
     */
    private void gotoBrowser(HomeBannerMadel model) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_URL, model.getUrl());
        bundle.putString(KEY_HEAD_URL, model.getHeadurl());
        bundle.putString(KEY_INFORMATIONID, model.getInformation_id());
        bundle.putString(KEY_TITLE, model.getTitle());
        goTargetActivity(GameInfoDetailActivity.class, bundle);
    }
}
